﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThoughtWorks.QRCode.Codec;

namespace Common
{
    public class QrcodeHelper
    {
        public static Bitmap CreateQrCode(string txtEncodeData)
        {
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();

            //设置编码模式 （BYTE，ALPHA_NUMERIC，NUMERIC）
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            //设置编码测量度  
            qrCodeEncoder.QRCodeScale = 6;
            //设置编码版本 
            qrCodeEncoder.QRCodeVersion = 0;
            //设置编码错误纠正  (L可遮7%,M可遮15%,Q可遮25%，H可遮30%）
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;

            qrCodeEncoder.QRCodeBackgroundColor = Color.White;
            qrCodeEncoder.QRCodeForegroundColor = Color.Black;
            qrCodeEncoder.setStructureappend(100, 200, 300);
            //生成二维码图片  
            Bitmap bitmap = qrCodeEncoder.Encode(txtEncodeData);

            return bitmap;
        }

        public static Bitmap CreateQrCode(string txtEncodeData, string frontText, Bitmap fixImage, Bitmap logoImage)
        {
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();

            //设置编码模式 （BYTE，ALPHA_NUMERIC，NUMERIC）
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            //设置编码测量度  
            qrCodeEncoder.QRCodeScale = 10;
            //设置编码版本 
            qrCodeEncoder.QRCodeVersion = 0;
            //设置编码错误纠正  (L可遮7%,M可遮15%,Q可遮25%，H可遮30%）
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;

            qrCodeEncoder.QRCodeBackgroundColor = Color.White;
            qrCodeEncoder.QRCodeForegroundColor = Color.Black;
            qrCodeEncoder.setStructureappend(100, 200, 300);
            //生成二维码图片  
            Bitmap bitmap = qrCodeEncoder.Encode(txtEncodeData);

            #region 图片混合
            if (fixImage != null)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        //像素范围相同
                        if (x < fixImage.Width && y < fixImage.Height)
                        {
                            var bitColor = bitmap.GetPixel(x, y);
                            var fixColor = fixImage.GetPixel(x, y);
                            //修改前景色
                            if (bitColor.ToArgb() == qrCodeEncoder.QRCodeForegroundColor.ToArgb())
                            {

                                double qrY = 0.257 * qrCodeEncoder.QRCodeBackgroundColor.R +
                                             0.504 * qrCodeEncoder.QRCodeBackgroundColor.G +
                                             0.098 * qrCodeEncoder.QRCodeBackgroundColor.B + 16;
                                double qrCb = -0.148 * qrCodeEncoder.QRCodeBackgroundColor.R -
                                              0.291 * qrCodeEncoder.QRCodeBackgroundColor.G +
                                              0.439 * qrCodeEncoder.QRCodeBackgroundColor.B + 128;
                                double qrCr = 0.439 * qrCodeEncoder.QRCodeBackgroundColor.R -
                                              0.368 * qrCodeEncoder.QRCodeBackgroundColor.G -
                                              0.071 * qrCodeEncoder.QRCodeBackgroundColor.B + 128;

                                double fixY = 0.257 * fixColor.R +
                                              0.504 * fixColor.G +
                                              0.098 * fixColor.B + 16;
                                double fixYCb = -0.148 * fixColor.R -
                                                0.291 * fixColor.G +
                                                0.439 * fixColor.B + 128;
                                double fixYCr = 0.439 * fixColor.R -
                                                0.368 * fixColor.G -
                                                0.071 * fixColor.B + 128;
                                //YCbCr带流明的色值计算阀值
                                if (Math.Abs((qrY + qrCb + qrCr) - (fixY + fixYCb + fixYCr)) >= 80)
                                //三元色的值差计算阀值
                                //if ((System.Math.Abs((qrCodeEncoder.QRCodeBackgroundColor.R + qrCodeEncoder.QRCodeBackgroundColor.G + qrCodeEncoder.QRCodeBackgroundColor.B) - (fixColor.R + fixColor.G + fixColor.B)) >= 203)
                                {
                                    Color color = fixImage.GetPixel(x, y);
                                    bitmap.SetPixel(x, y, color);
                                }
                            }
                        }
                    }
                }

                fixImage.Dispose();
            }
            #endregion

            #region LOGO合成
            if (logoImage != null)
            {


                Bitmap bmp = new Bitmap(logoImage.Width, logoImage.Height);
                Graphics g = Graphics.FromImage(bmp);
                //画图像
                g.DrawImage(logoImage, 0, 0);
                //画字符
                //SolidBrush brush = new SolidBrush(frontColorPanel.BackColor);
                //g.DrawString(frontText.Text, new Font("Arial", 35), brush, 0.0F, 0.0F);

                //brush.Dispose();
                g.Dispose();
                //img.Dispose();
                //
                int lx = bitmap.Width / 2 - logoImage.Width / 2, ly = bitmap.Height / 2 - logoImage.Height / 2;

                for (int x = 0; x < logoImage.Width; x++)
                {
                    for (int y = 0; y < logoImage.Height; y++)
                    {
                        System.Drawing.Color color = logoImage.GetPixel(x, y);
                        bitmap.SetPixel(lx + x, ly + y, color);
                    }
                }

                logoImage.Dispose();
            }
            #endregion


            return bitmap;
        }
    }
}
